import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:ble2025_plugin_example/util/app_all_value.dart';
import 'package:ble2025_plugin_example/util/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

///设备信息页面
class DeviceInformationPage extends StatefulWidget {
  const DeviceInformationPage({Key? key}) : super(key: key);

  @override
  State<DeviceInformationPage> createState() => _DeviceInformationPageState();
}

class _DeviceInformationPageState extends State<DeviceInformationPage> {
  AppAllValueController controller = Get.find();

  bool ancs = false;

  var baseHeartController = TextEditingController();
  var motorVibrationController = TextEditingController();
  var indexDialController = TextEditingController();
  var screenBrightnessController = TextEditingController();

  @override
  void initState() {
    super.initState();
    EventBus().on('dataCallBack', (arg) {
      Map map = arg;
      switch (map[DeviceKey.DataType] as String) {
        case BleConst.GetDeviceBatteryLevel:
          controller.showContentDialog(map.toString());
          break;
        case BleConst.CMD_MCUReset:
          controller.showContentDialog(map.toString());
          break;
        case BleConst.GetDeviceMacAddress:
          controller.showContentDialog(map.toString());
          break;
        case BleConst.GetDeviceVersion:
          controller.showContentDialog(map.toString());
          break;
        case BleConst.GetDeviceInfo:
          setData(map[DeviceKey.Data]);
          // controller.showContentDialog(map.toString());
          break;
        case BleConst.SetDeviceInfo:
          controller.showContentDialog(map.toString());
          break;
      }
    });
  }

  void setData(Map map){
    setState(() {
      if("0" == map[DeviceKey.AncsEnable]){
        ancs = false;
      }else{
        ancs = true;
      }
    });
    baseHeartController.text = map[DeviceKey.KBaseHeart];
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("设备信息".tr),
        ),
        body: ListView(
          shrinkWrap: false,
          padding: const EdgeInsets.only(top: 0),
          children: [
            Row(
              children: [
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(left: 10, right: 10),
                  child: ElevatedButton(
                    child: Text('初始化'.tr),
                    onPressed: () {
                      controller.showContentDialogTitle(
                          '提示'.tr, '出厂重置将清除设备中的所有数据，请确认是否要重置？'.tr,
                          onConfirm: () {
                        controller.writeData(BleSDK.Reset());
                      });
                    },
                  ),
                )),
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(right: 10),
                  child: ElevatedButton(
                    child: Text('电量'.tr),
                    onPressed: () {
                      controller.writeData(BleSDK.GetDeviceBatteryLevel());
                    },
                  ),
                ))
              ],
            ),
            Row(
              children: [
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(left: 10, right: 10),
                  child: ElevatedButton(
                    child: Text('蓝牙Mac地址'.tr),
                    onPressed: () {
                      controller.writeData(BleSDK.GetDeviceMacAddress());
                    },
                  ),
                )),
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(right: 10),
                  child: ElevatedButton(
                    child: Text('固件版本'.tr),
                    onPressed: () {
                      controller.writeData(BleSDK.GetDeviceVersion());
                    },
                  ),
                ))
              ],
            ),
            Row(
              children: [
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(left: 10, right: 10),
                  child: ElevatedButton(
                    child: Text('MCU重启'.tr),
                    onPressed: () {
                      controller.writeData(BleSDK.MCUReset());
                    },
                  ),
                )),
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(right: 10),
                  child: ElevatedButton(
                    child: Text('进入升级模式'.tr),
                    onPressed: () {
                      controller.writeData(BleSDK.enterOTA());
                    },
                  ),
                ))
              ],
            ),
            Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Text(' ${'ANCS'.tr}   '),
                Switch(
                    value: ancs,
                    onChanged: (value) {
                      setState(() {
                        ancs = value;
                      });
                    }),
              ],
            ),
            TextField(
              controller: baseHeartController,
              textAlign: TextAlign.center,
              decoration: InputDecoration(hintText: '基础心率必须大于40'.tr),
              keyboardType: TextInputType.number,
              textInputAction: TextInputAction.next,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9]')), //只允许输入数
              ],
            ),
            Row(
              children: [
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(left: 10, right: 10),
                  child: ElevatedButton(
                    child: Text(
                      '设置设备信息'.tr,
                      textAlign: TextAlign.center,
                    ),
                    onPressed: () {
                      if (baseHeartController.text.isNotEmpty) {
                        int hr = int.parse(baseHeartController.text);
                        if(hr > 40){
                          var info = MyDeviceInfo(ancs: ancs,baseheart: hr);
                          controller.writeData(BleSDK.SetDeviceInfo(info));
                        }else{
                          controller.showContentDialog("基础心率必须大于40".tr);
                        }
                      }else{
                        controller.showContentDialog("基础心率没有填".tr);
                      }
                    },
                  ),
                )),
                Expanded(
                    child: Container(
                  margin: const EdgeInsets.only(right: 10),
                  child: ElevatedButton(
                    child: Text(
                      '获取设备信息'.tr,
                      textAlign: TextAlign.center,
                    ),
                    onPressed: () {
                      controller.writeData(BleSDK.GetDeviceInfo());
                    },
                  ),
                ))
              ],
            ),
            Container(
              alignment: Alignment.center,
              child: Text('手环震动频率'.tr),
            ),
            TextField(
              controller: motorVibrationController,
              textAlign: TextAlign.center,
              decoration: const InputDecoration(hintText: '0-5'),
              keyboardType: TextInputType.number,
              textInputAction: TextInputAction.next,
              maxLength: 1,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp('[0-9]')), //只允许输入数
              ],
            ),
            ElevatedButton(
              child: Text('设置'.tr),
              onPressed: () {
                if (motorVibrationController.text.isNotEmpty) {
                  int v = int.parse(motorVibrationController.text);
                  controller.writeData(BleSDK.MotorVibrationWithTimes(v));
                }
              },
            )
          ],
        ));
  }

  @override
  void dispose() {
    super.dispose();
    EventBus().off('dataCallBack');
  }
}
